﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GeneralLibs;
namespace Lists
{
    public class NguoiDuocPV : DoiTuong
    {
        #region HoTen property
        public string HoTen
        {
            get
            {
                if (_DataRow["HoTen"] is string) return (string)_DataRow["HoTen"];
                else return "";
            }
            set
            {
                _DataRow["HoTen"] = value;
            }
        }
        #endregion

        #region GioiTinh property
        public bool GioiTinh
        {
            get
            {
                if (_DataRow["GioiTinh"] is bool) return (bool)_DataRow["GioiTinh"];
                return false;
            }
            set
            {
                _DataRow["GioiTinh"] = value;
            }
        }
        #endregion

        #region NamSinh property
        public int NamSinh
        {
            get
            {
                if (_DataRow["NamSinh"] is int) return (int)_DataRow["NamSinh"];
                return 0;
            }
            set
            {
                _DataRow["NamSinh"] = value;
            }
        }
        #endregion

        #region Tuoi property
        public int Tuoi
        {
            get
            {
                if (_DataRow["Tuoi"] is int) return (int)_DataRow["Tuoi"];
                return 0;
            }
            set
            {
                _DataRow["Tuoi"] = value;
            }
        }
        #endregion

        #region DienThoai property
        public string DienThoai
        {
            get
            {
                if (_DataRow["DienThoai"] is string) return (string)_DataRow["DienThoai"];
                else return "";
            }
            set
            {
                _DataRow["DienThoai"] = value;
            }
        }
        #endregion

        #region Phieu property
        public Phieu Phieu
        {
            get
            {
                if (_DataRow["PhieuID"] is Guid)
                    return (Phieu)DanhMucPhieu.Instance.GetByID((Guid)_DataRow["PhieuID"]);
                else return null;
            }
            set
            {
                if (value is Phieu)
                    _DataRow["PhieuID"] = value.ID;
                else
                    _DataRow["PhieuID"] = DBNull.Value;
            }
        }
        #endregion

        #region TrinhDoPT property
        public TrinhDoPT TrinhDoPT
        {
            get
            {
                if (_DataRow["TrinhDoPTID"] is Guid)
                    return (TrinhDoPT)DanhMucTrinhDoPT.Instance.GetByID((Guid)_DataRow["TrinhDoPTID"]);
                else return null;
            }
            set
            {
                if (value is TrinhDoPT)
                    _DataRow["TrinhDoPTID"] = value.ID;
                else
                    _DataRow["TrinhDoPTID"] = DBNull.Value;
            }
        }
        #endregion

        #region TrinhDoKT property
        public TrinhDoKT TrinhDoKT
        {
            get
            {
                if (_DataRow["TrinhDoKTID"] is Guid)
                    return (TrinhDoKT)DanhMucTrinhDoKT.Instance.GetByID((Guid)_DataRow["TrinhDoKTID"]);
                else return null;
            }
            set
            {
                if (value is TrinhDoKT)
                    _DataRow["TrinhDoKTID"] = value.ID;
                else
                    _DataRow["TrinhDoKTID"] = DBNull.Value;
            }
        }
        #endregion

        #region Nghe property
        public Nghe Nghe
        {
            get
            {
                if (_DataRow["NgheID"] is Guid)
                    return (Nghe)DanhMucNghe.Instance.GetByID((Guid)_DataRow["NgheID"]);
                else return null;
            }
            set
            {
                if (value is Nghe)
                    _DataRow["NgheID"] = value.ID;
                else
                    _DataRow["NgheID"] = DBNull.Value;
            }
        }
        #endregion

        #region ChucVu property
        public string ChucVu
        {
            get
            {
                if (_DataRow["ChucVu"] is string) return (string)_DataRow["ChucVu"];
                else return "";
            }
            set
            {
                _DataRow["ChucVu"] = value;
            }
        }
        #endregion

        #region NamLamViec property
        public string NamLamViec
        {
            get
            {
                if (_DataRow["NamLamViec"] is string) return (string)_DataRow["NamLamViec"];
                else return "";
            }
            set
            {
                _DataRow["NamLamViec"] = value;
            }
        }
        #endregion

        #region ThangLamViec property
        public string ThangLamViec
        {
            get
            {
                if (_DataRow["ThangLamViec"] is string) return (string)_DataRow["ThangLamViec"];
                else return "";
            }
            set
            {
                _DataRow["ThangLamViec"] = value;
            }
        }
        #endregion

        #region IsQuanLy property
        public bool IsQuanLy
        {
            get
            {
                if (_DataRow["IsQuanLy"] is bool) return (bool)_DataRow["IsQuanLy"];
                return false;
            }
            set
            {
                _DataRow["IsQuanLy"] = value;
            }
        }
        #endregion

        public NguoiDuocPV(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucNguoiDuocPV : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucNguoiDuocPV _Instance;
        public static DanhMucNguoiDuocPV Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucNguoiDuocPV();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "NguoiTraLoiPV";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new NguoiDuocPV((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public NguoiDuocPV this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new NguoiDuocPV(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public NguoiDuocPV NewDoiTuong()
        {
            return new NguoiDuocPV(DataTable.NewRow());
        }

        public NguoiDuocPV GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new NguoiDuocPV(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public NguoiDuocPV GetByID(Guid objID)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            //return GetByProperty("ID", objID);
        }

        public static DanhMucNguoiDuocPV GetAll()
        {
            return Instance;
        }

        public static DanhMucNguoiDuocPV Filter(string filterExpression)
        {
            try
            {
                DanhMucNguoiDuocPV obj = new DanhMucNguoiDuocPV();
                obj.DataView = new DataView(DanhMucNguoiDuocPV.Instance.DataTable, filterExpression, "Ten", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static NguoiDuocPV GetByHoTen(string hoTen)
        {
            try
            {
                return DanhMucNguoiDuocPV.Instance.GetByProperty("HoTen", hoTen.Trim());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
